package com.xuanwei.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuanwei.core.entity.bo.WxUserDetail;
import com.xuanwei.core.exception.XwException;
import com.xuanwei.core.service.IXwUserService;
import com.xuanwei.core.util.ParamUtil;
import com.xuanwei.dao.entity.XwUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/4/22 19:01
 **/
@Slf4j
public class BasicController {

    @Autowired
    private IXwUserService userService;

    Integer getUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication.getPrincipal().equals("anonymousUser")) {
            return null;
        }
        WxUserDetail principal = (WxUserDetail) authentication.getPrincipal();
        return Integer.parseInt(principal.getUsername());
    }

    XwUser getUser() {
        return userService.getBaseMapper().selectById(getUserId());
    }


    <T> Page<T> getPage() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();

        String index = request.getParameter("index");
        String pageSize = request.getParameter("pageSize");
        if (StringUtils.isBlank(index)) {
            index = "1";
        }

        if (StringUtils.isBlank(pageSize)) {
            pageSize = "10";
        }

        return new Page<>(Integer.parseInt(index), Integer.parseInt(pageSize));
    }

    <T> Page<T> getPage(Integer index, Integer pageSize) {
        if (!ParamUtil.checkIsPositiveInteger(index)) {
            index = 1;
        }
        if (!ParamUtil.checkIsPositiveInteger(pageSize)) {
            pageSize = 20;
        }

        return new Page<T>(index, pageSize);
    }

    /**
     * 从request中获取请求方IP
     *
     * @return
     */
    String getIpAddress() {
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    String getIpAddress(String ip) {
        try {
            Document document = Jsoup.connect("http://www.ip138.com/ips138.asp?ip=" + ip + "&action=2").get();
            return document.body().getElementsByTag("li").get(0).wholeText().replace("本站数据：", "");
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}
